/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entity.Users;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import sessionbeans.UsersFacade;

/**
 *
 * @author Cyril
 */
@ManagedBean(name = "User")
@RequestScoped
public class UserBean {

    /** Creates a new instance of User */
    public UserBean() {
    }
    
    @EJB
    private UsersFacade uf;

    private String name = "";
    private String password = "";
    
    @ManagedProperty(value="#{userBean}") // #{user} is the managed bean name
    private sessionUserBean userBean;

    @ManagedProperty(value="#{userTempBean}") // #{user} is the managed bean name
    private sessionUserTempBean userTempBean;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public String inscription() {

        String result = "error";
        
        try {
            //vérifie si le user exist
            if(uf.getName(this.name) == null){
                Users user = new Users();
                user.setName(name);
                user.setPassword(password);
                uf.create(user); 
                
                getUserBean().setUser(user);
                getUserTempBean().setUser(user);
                result = "indexConnect";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result; 
    }
    
    public String connexion(){
        
        String result = "error";
        Users user = uf.getName(name);
        
        if(( null!=user)){
            if(user.getPassword().equals(password))
                result = "indexConnect";
        }
        
        return result;
    }

    /**
      * @return the userBean
     */
    public sessionUserBean getUserBean() {
        return userBean;
    }

    /**
     * @param userBean the userBean to set
     */
    public void setUserBean(sessionUserBean userBean) {
        this.userBean = userBean;
    }

    /**
     * @return the userTempBean
     */
    public sessionUserTempBean getUserTempBean() {
        return userTempBean;
    }

    /**
     * @param userTempBean the userTempBean to set
     */
    public void setUserTempBean(sessionUserTempBean userTempBean) {
        this.userTempBean = userTempBean;
    }
    
}
